{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bdb9721a",
   "metadata": {},
   "source": [
    "# Kokoro Text To Speech\n",
    "\n",
    "* Kokoro is a light weight, but strong Text to Speach Library on Hugging Face\n",
    "    * [HuggingFace: hexgrad/Kokoro-82M](https://huggingface.co/hexgrad/Kokoro-82M/tree/main)\n",
    "* Python Setup\n",
    "    * This video describes setup: [Kokoro-82M: Install and Run Locally Fast, Small, and Free Text to Speech AI Model Kokoro-82M](https://www.youtube.com/watch?v=bs45W7CEGps)\n",
    "    * Install espeak (use the espeak-ng.msi installer on windows): [Espeak Releases](https://github.com/espeak-ng/espeak-ng/releases)\n",
    "    * Install python packages in the LLM project using UV:\n",
    "        * uv add kokoro\n",
    "        * uv add soundfile\n",
    "        * uv add https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.8.0/en_core_web_sm-3.8.0.tar.gz\n",
    "* Kokoro Voice Options\n",
    "    * [Dozens of voice options](https://huggingface.co/hexgrad/Kokoro-82M/tree/main/voices)\n",
    "    * Voices are prefixed with af or am, for american female or american male\n",
    "    * Voices are prefixed with bf or bm for british female or british male "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23acd8a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from kokoro import KPipeline\n",
    "import soundfile as sf\n",
    "# US 'a' => American English, GB 'b' British English\n",
    "# JP 'j' => Japanese (uv pip install additional language models separately - misaki[ja])\n",
    "# CN 'z' => Mandarin Chinese (uv pip install additional language models separately - misaki[zh])\n",
    "\n",
    "try:\n",
    "    pipeline = KPipeline(lang_code='a')\n",
    "except SystemExit as se:\n",
    "    print(\"KPipeline initialization failed. Ensure en_core_web_sm is pip installed.\")\n",
    "except Exception as e:\n",
    "    print(\"Error initializing Kokoro pipeline:\", e)\n",
    "    \n",
    "text = '''\n",
    "Hello, this is a test of the Kokoro text to speech system.\n",
    "This is an example of generating speech from text using a lightweight model.\n",
    "Kokoro is designed to be efficient and effective for various text-to-speech applications.\n",
    "Enjoy exploring the capabilities of Kokoro for your text-to-speech needs!\n",
    "'''\n",
    "generator = pipeline(\n",
    "    text,\n",
    "    voice = 'af_heart', #'hf_alpha', #'ff_siwis', #'bm_george', #'bm_daniel', #'bf_lily', #'bf_isabella', #'bf_emma','hm_omega' , #'am_eric',\n",
    "    speed = 1.0,\n",
    "    split_pattern=r'\\n+'\n",
    ")\n",
    "for i, (gs, ps, audio) in enumerate(generator):\n",
    "    print(i) # i => index of chunk\n",
    "    print(gs) # gs =< graphemes/text\n",
    "    print(ps) #ps => phonemes\n",
    "    sf.write(f'./audioOutput/kokoro_output_{i}.wav', audio, 24000) #save each audio file chunk\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a055c79",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm-engineering",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
